//
//  resultViewController.m
//  RealEstateQa
//
//  Created by 张立彬 on 13-7-14.
//  Copyright (c) 2013年 com.rong360. All rights reserved.
//

#import "resultViewController.h"
#import "RealEstateQaViewController.h"
//加载友盟分享
#import "UMSocial.h"

@interface resultViewController ()

@end

@implementation resultViewController

@synthesize Mc;

@synthesize resultList;

@synthesize shareText;

@synthesize ResultIcon;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) didTapBackButton:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT);
    //适配iphone5显示
    self.title = @"测试结果";
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setBackgroundImage:[UIImage imageNamed:@"navi_backbtn"] forState:UIControlStateNormal];
    [backButton setTitle:@"" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
    backButton.frame = CGRectMake(0.0f, 0.0f, 25.0f, 25.0f);
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self loadScroll];
    CGRect _frame = [UIScreen mainScreen].applicationFrame;
    if(_frame.size.height > 480){
        CGRect newFrame = Resultnfo.frame;
        newFrame.size = CGSizeMake(320, 100);
        Resultnfo.frame = newFrame;
    }

    //渲染计算结果
    //NSLog(@"xxxxxxx%@",resultList);
    //NSLog(@"%@",resultList[@"resultList"]);
    Resultnfo = [[UIView alloc]initWithFrame:CGRectMake(0, _frame.size.height/2+1, 320, _frame.size.height/2+1)];
    Resultnfo.backgroundColor = [UIColor colorWithRed:(237/255.0) green:(242/255.0) blue:(245/255.0) alpha:1];
    UIButton *btnRecal = [[UIButton alloc]initWithFrame:CGRectMake(15, Resultnfo.frame.size.height-100, 290, 38)];//changed by lwtian 14-02-07
    //BtnRecal.frame = CGRectMake(15, Resultnfo.frame.size.height-108, 290, 38);
    [btnRecal setImage:[UIImage imageNamed:@"recal"] forState:UIControlStateNormal];
    [btnRecal addTarget:self action:@selector(ReCal:) forControlEvents:UIControlEventTouchUpInside];
    [ScrollBody addSubview:Resultnfo];
    [Resultnfo addSubview:btnRecal];
    [ScrollBody addSubview:ResultIcon];
    [ScrollBody bringSubviewToFront:self.view];
    [Resultnfo bringSubviewToFront:ScrollBody];
    
    //加载结果显示
    DataTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 20)];
    [DataTitle setFont:[UIFont fontWithName:@"Helvetica" size:22]];
    
    UIButton *Btnshare = [[UIButton alloc]initWithFrame:CGRectMake(260, 16, 50, 30)];
    //BtnRecal.frame = CGRectMake(15, Resultnfo.frame.size.height-108, 290, 38);
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(15,5,20,20)];
    icon.image = [UIImage imageNamed:@"share_1.png"];
    [Btnshare addSubview:icon];
    //[Btnshare setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    //[Btnshare addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    [Btnshare addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    [ScrollBody addSubview:Btnshare];
    
    //加载利率
    //加载标题
    //加载标题
    ResultLilv = [[UILabel alloc] initWithFrame:CGRectMake(0, Resultnfo.frame.size.height-25, 320, 20)];
    //设置字体
    [ResultLilv setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    //设置背景为透明
    [ResultLilv setBackgroundColor:[UIColor clearColor]];
    //写入文本
    [ResultLilv setText:@"基准利率：6.55%"];
    ResultLilv.textAlignment = UITextAlignmentCenter;
    ResultLilv.textColor = [UIColor grayColor];
    [ScrollBody addSubview:ResultLilv];
    
    //设置背景为透明
    [DataTitle setBackgroundColor:[UIColor clearColor]];
    DataTitle.textColor = [UIColor colorWithRed:(231/255.0) green:(76/255.0) blue:(60/255.0) alpha:1];
    DataTitle.textAlignment = UITextAlignmentCenter;

    [ScrollBody addSubview:DataTitle];
    
    int itemCount = 0;
    for(int key = 0; key < [resultList[@"listKey"] count];key++){
        //加载标题
        //加载标题
        UILabel *label = [[UILabel alloc] init];
        //设置字体
        [label setFont:[UIFont fontWithName:@"Helvetica" size:13]];
        //设置背景为透明
        [label setBackgroundColor:[UIColor clearColor]];
        NSString *text = resultList[@"resultList"][resultList[@"listKey"][key]][@"title"];
        //写入文本
        [label setText:text];

        CGSize wrapSize = [text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, label.frame.size.height)];
        
        UILabel *tail = [[UILabel alloc] init];

        //设置字体
        [tail setFont:[UIFont fontWithName:@"Helvetica" size:13]];
        //设置背景为透明
        [tail setBackgroundColor:[UIColor clearColor]];
        tail.textColor = [UIColor colorWithRed:(26/255.0) green:(173/255.0) blue:(242/255.0) alpha:1];
        NSString *tailText = resultList[@"resultList"][resultList[@"listKey"][key]][@"varText"];
        CGSize subSize = [text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 20)];
        //写入文本
        [tail setText:tailText];
        NSLog(@"字数：%i",[tailText length]);
        
        //适配显示
        int mainCount = text.length;
        int mainCountX;
        int subCount = tailText.length;
        NSString *mainText;
        if((mainCount+subCount) > 22){
            mainCountX = 22 - subCount;
            mainText = [NSString stringWithFormat:@"%@...:",[text substringToIndex:mainCountX]];
            mainCountX = mainCountX + 2;
        }else{
            mainCountX = mainCount;
            mainText = text;
        }
        /*
        NSLog(@"tailwidth:%f",(wrapSize.width + subSize.width));
        if((wrapSize.width + subSize.width)>500){
            label.frame = CGRectMake(5, itemCount*25+10, 330-subSize.width/2, 20);
        }else{
            label.frame = CGRectMake(5, itemCount*25+10, wrapSize.width, 20);
        }
         */
        label.text = mainText;
        label.frame = CGRectMake(5, itemCount*25+10, mainCountX*13, 20);
        [Resultnfo addSubview:label];
        
        tail.frame = CGRectMake(label.frame.size.width, itemCount*25+10, subSize.width*2.5, 20);
        
        //label.frame = CGRectMake(5, itemCount*25+10, wrapSize.width, 20);
        

        //tail.frame = CGRectMake(label.frame.size.width, itemCount*25+10, subSize.width, 20);
    
        [Resultnfo addSubview:tail];
       

        
        //加载答案
        itemCount++;
    }
    
    //如果失败
    if([resultList[@"type"] isEqual: @"fail"]){
        DataTitle.text = @"您目前没有购房资格";
        Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-92)/2, 50, 92, 92)];
        [Icon setImage:[UIImage imageNamed:@"home_null.png"]];
        [ResultIcon addSubview:Icon];
        ResultLilv.hidden = YES;
        NSLog(@"城市：%@",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]);
        shareText = [NSString stringWithFormat:@"不能在%@买房",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
    }
    
    //如果成功
    if([resultList[@"type"] isEqual: @"success"]){
        //获取canbuy值
        int canyBuy = [resultList[@"canBuy"] intValue];
        //获取现在有几套房值
        int numHave = [resultList[@"nowHave"] intValue];
        //获取算做第几套房值
        int nowCount = [resultList[@"nowCount"] intValue];
        //ResultLilv.frame = CGRectMake(100, 220, 80, 20);
        //如果能买两套房，有0条贷款记录
        if((canyBuy - numHave) == 2 && nowCount == 1){
            //DataTitle.text = [NSString stringWithFormat:@"您可以购买%i套房", (canyBuy - numHave)];
            DataTitle.text = @"您可以购买两套房";
            Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-303)/2, 20, 303, 133)];
            [Icon setImage:[UIImage imageNamed:@"case0.png"]];
            [ResultIcon addSubview:Icon];
            //分享文案
            shareText = [NSString stringWithFormat:@"可以在%@买房，首付3成，利率85折",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
        }

        //如果能买两套房，有1条贷款记录
        if((canyBuy - numHave) == 2 && nowCount == 2){
            //DataTitle.text = [NSString stringWithFormat:@"您可以购买%i套房", (canyBuy - numHave)];
            DataTitle.text = @"您可以购买两套房";
            Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-253)/2, 20, 253, 133)];
            [Icon setImage:[UIImage imageNamed:@"case1.png"]];
            [ResultIcon addSubview:Icon];
            //分享文案
            shareText = [NSString stringWithFormat:@"可以在%@买房，首付7成，利率1.1",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
        }
        //如果能买两套房，有2条贷款记录
        if((canyBuy - numHave) == 2 && nowCount == 3){
            //DataTitle.text = [NSString stringWithFormat:@"您可以购买%i套房", (canyBuy - numHave)];
            DataTitle.text = @"您可以购买两套房";
            Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-240)/2, 20, 240, 134)];
            [Icon setImage:[UIImage imageNamed:@"case2.png"]];
            [ResultIcon addSubview:Icon];
            ResultLilv.hidden = YES;
            //分享文案
            shareText = [NSString stringWithFormat:@"只能在%@全款买房",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
        }

        //如果能买1套房，有1条贷款记录
        if((canyBuy - numHave) == 1 && nowCount == 2){
            //DataTitle.text = [NSString stringWithFormat:@"您可以购买%i套房", (canyBuy - numHave)];
            DataTitle.text = @"您可以购买一套房";
            Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-133)/2, 20, 133, 143)];
            [Icon setImage:[UIImage imageNamed:@"case3.png"]];
            [ResultIcon addSubview:Icon];
            //分享文案
            shareText = [NSString stringWithFormat:@"可以在%@买房，首付7成，利率1.1",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
        }

        //如果能买1套房
        if((canyBuy - numHave) == 1 && nowCount == 1){
            //DataTitle.text = [NSString stringWithFormat:@"您可以购买%i套房", (canyBuy - numHave)];
            DataTitle.text = @"您可以购买一套房";
            Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-150)/2, 20, 150, 130)];
            [Icon setImage:[UIImage imageNamed:@"home_can.png"]];
            [ResultIcon addSubview:Icon];
            //分享文案
            shareText = [NSString stringWithFormat:@"可以在%@买房，首付3成，利率8.8折",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
        }
    
        //如果能买1套房，不能贷款
        if((canyBuy - numHave) == 1 && nowCount == 3){
            //DataTitle.text = [NSString stringWithFormat:@"您可以购买%i套房", (canyBuy - numHave)];
            DataTitle.text = @"您可以购买一套房";
            Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-92)/2, 30, 92, 132)];
            [Icon setImage:[UIImage imageNamed:@"case4.png"]];
            [ResultIcon addSubview:Icon];
            //分享文案
            shareText = [NSString stringWithFormat:@"可以在%@买房，不能贷款",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
            ResultLilv.hidden = YES;
        }
        
        //如果能买0套房，不能贷款
        if((canyBuy - numHave) == 0){
            DataTitle.text = @"您目前没有购房资格";
            Icon= [[UIImageView alloc]initWithFrame:CGRectMake((320-92)/2, 20, 92, 92)];
            [Icon setImage:[UIImage imageNamed:@"home_null.png"]];
            [ResultIcon addSubview:Icon];
            ResultLilv.hidden = YES;
            NSLog(@"城市：%@",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]);
            shareText = [NSString stringWithFormat:@"不能在%@买房",resultList[@"resultList"][resultList[@"listKey"][0]][@"varText"]];
            ResultLilv.hidden = YES;
        }


    }
}

//加载滚动条
-(void)loadScroll{
    UIScrollView *s = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    ScrollBody = s;
    s.pagingEnabled = YES;
    s.bounces = YES;
    s.showsVerticalScrollIndicator = NO;
    s.showsHorizontalScrollIndicator = NO;
    [s setContentSize:CGSizeMake(320,self.view.frame.size.height+40)];
    [self.view addSubview:s];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//重新计算
-(IBAction)ReCal:(id)sender{
    Mc.cIndex = 9999;
    [[self navigationController] popViewControllerAnimated:YES];
}
//分享
-(IBAction)share:(id)sender{
    NSString *_shareText = [NSString stringWithFormat:@"我的#房贷资格测试#结果：“%@”。你也来测试一下吧，测试地址：http://www.rong360.com/calculator/xiangou。",shareText];
    
    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
    [UMSocialWechatHandler setWXAppId:kWXAppID url:kRong360Host];
    [UMSocialConfig setQQAppId:kTencentID url:kRong360Host importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
//    [UMSocialConfig setShareQzoneWithQQSDK:YES url:kRong360Host importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:kUMSocialKey
                                      shareText:_shareText
                                     shareImage:[UIImage imageNamed:kShareIcon]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,nil]
                                       delegate:nil];
}

@end
